package com.yb.nowcoder;

public class Solution {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int k1 = 1,k2 = 1;
        ListNode current1 = l1,current2 = l2;
        int num1 = 0,num2 = 0;
        while(current1!=null){
            num1 +=current1.val*k1;
            current1 = current1.next;
            k1 *=10;
        }
        while(current2!=null){
            num2 +=current2.val*k2;
            current2 = current2.next;
            k2 *=10;
        }
        int sum = num1+num2;

        int k3 = 10;
        int temp;
        ListNode head = null;
        ListNode node = null;
        while(sum>0){
            temp = sum%k3;
            if (k3 == 10){
                head = new ListNode(temp);
                node = head;
            }
            node.next = new ListNode(temp);
            node = node.next;
            sum -=temp;
            k3 *=10;
        }
        return head;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
